#include "channel.h"

#include "gl_triangle_mesh.h"
#include "window.h"

eqtest::Channel::Channel(eq::Window* parent): eq::Channel(parent)
{
}

bool eqtest::Channel::configInit(const eq::uint128_t& initID)
{
    if(!eq::Channel::configInit(initID))
        return false;
    
    return this->getLocalNode()->mapObject(&state, initID);
}

void eqtest::Channel::frameDraw(const eq::uint128_t& frameID)
{
    eq::Channel::frameDraw(frameID);
    
    state.sync(frameID);
    
    glDisable(GL_LIGHTING);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    
    static struct
    { GLdouble const x, y, z; } viewpoint = {0, 0, 3};
    
    glTranslated(0, 0, -3.0);
    glRotatef(state.rotate.x, 1.0f, 0.0f, 0.0f);
    glRotatef(state.rotate.y, 0.0f, 1.0f, 0.0f);
    
    #if 0
    glColor3f(state.r, state.g, state.b);
    glBegin(GL_QUADS);
    glVertex2f(-1, -1);
    glVertex2f(1, -1);
    glVertex2f(1, 1);
    glVertex2f(-1, 1);
    glEnd();
    #endif
    
    glScaled(0.01, 0.01, 0.01);
    Window *window = dynamic_cast<Window*>(this->getWindow());
    ShaderProgram &shader = window->getShader();
    
    shader.bind();
    
    glUniform3f(shader.uniform("lightPos"), state.light.x, state.light.y, state.light.z);
    
    TextureBinding
    bindAlbedo  (window->getAlbedo()  , 0),
    bindNormal  (window->getNormal()  , 1),
    bindSpecular(window->getSpecular(), 2),
    bindStretch (window->getStretch() , 3),
    bindRho_d   (window->getRho_d()   , 4);
    
    window->getMesh().draw();
    shader.release();
}
